package com.sgb.kjwl.viewmodel.manage;

import android.content.Context;
import android.text.TextUtils;

import androidx.lifecycle.MutableLiveData;

import com.blankj.utilcode.util.RegexUtils;
import com.sgb.kjwl.api.APIManager;
import com.swgk.core.base.api.ICallBack;
import com.sgb.kjwl.api.UrlConfig;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.kjwl.model.entity.manage.GloryHonorAdminEntity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * @author 张驰
 * @date 2021/5/17 14:57
 * @Description 荣誉荣耀的Model
 * @name com.sgb.kjwl.viewmodel.manage
 */
public class WebsiteUpDataRongYuRongYaoModel extends BaseViewModel {

    //知识产权详情
    private MutableLiveData<GloryHonorAdminEntity> gloryHonorAdminEntityData = new MutableLiveData<>(new GloryHonorAdminEntity());
    //荣誉分类
    private MutableLiveData<List<PickSelectEntity<List<PickSelectEntity>>>> honorPickSelectList = new MutableLiveData<>(new ArrayList<>());
    //荣誉荣耀行业分类
    private MutableLiveData<List<PickSelectEntity>> typePickSelectList = new MutableLiveData<>(new ArrayList<>());

    private MutableLiveData<String> urlString = new MutableLiveData<>("");

    public MutableLiveData<String> getUrlString() {
        return urlString;
    }

    public MutableLiveData<GloryHonorAdminEntity> getMutableLiveData() {
        return gloryHonorAdminEntityData;
    }

    public MutableLiveData<List<PickSelectEntity<List<PickSelectEntity>>>> getHonorPickSelectList() {
        return honorPickSelectList;
    }

    public MutableLiveData<List<PickSelectEntity>> getTypePickSelectList() {
        return typePickSelectList;
    }

    /**
     * 荣誉荣耀详情
     *
     * @param id
     */
    public void getGloryHonorAdminDetailById(Context mActivity, String id) {
        DialogHelper.showProgressDialog(mActivity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().changeAPIManager(UrlConfig.NEW_BASE_URL);
        APIManager.getInstance().getWebsiteInfoAPI().getGloryHonorAdminDetailById(id).enqueue(new ICallBack<BaseEntity<GloryHonorAdminEntity>>() {
            @Override
            public void onSuccess(BaseEntity<GloryHonorAdminEntity> data) {
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    gloryHonorAdminEntityData.postValue(data.getData());
                }
            }
        });
        APIManager.getInstance().changeAPIManager(UrlConfig.BASE_URL);
    }

    /**
     * 编辑或新增
     */
    public void gloryHonorAdminsaveOrUpdate(Context mActivity, Map<String, Object> map) {
        DialogHelper.showProgressDialog(mActivity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().changeAPIManager(UrlConfig.NEW_BASE_URL);
        APIManager.getInstance().getWebsiteInfoAPI().gloryHonorAdminsaveOrUpdate(map).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    getMessage().postValue("操作成功");
                }
            }
        });
        APIManager.getInstance().changeAPIManager(UrlConfig.BASE_URL);
    }


    /**
     * 荣誉分类
     */
    public void getHonorlist(Context mActivity,String message) {
        DialogHelper.showProgressDialog(mActivity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getWebsiteInfoAPI().getHonorlist().enqueue(new ICallBack<BaseEntity<List<PickSelectEntity<List<PickSelectEntity>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<PickSelectEntity<List<PickSelectEntity>>>> data) {
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    if (!TextUtils.isEmpty(message) && message.equals("数据回显")){
                        honorPickSelectList.setValue(data.getData());
                        getMessage().postValue(message);
                    }else{
                        honorPickSelectList.postValue(data.getData());
                    }
                }
            }
        });
    }

    /**
     * 行业分类
     *
     * @param id
     */
    public void getAllCategory(Context mActivity, String id) {
        DialogHelper.showProgressDialog(mActivity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getWebsiteInfoAPI().getAllCategory().enqueue(new ICallBack<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<PickSelectEntity>> data) {
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    typePickSelectList.postValue(data.getData());
                }
            }
        });
    }


    public void uploadAvatar(Context mContext, File file) {
        DialogHelper.showProgressDialog(mContext, null, "图片上传中...", 0, false, null).setCanceledOnTouchOutside(true);
        //创建map集合
        Map<String, RequestBody> photos = new HashMap<>();
        //将文件转换成多媒体文件，方便上传
        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        photos.put("file" + "\"; filename=\"" + RegexUtils.getReplaceAll(file.getName(), "[\u4e00-\u9fa5]", "") + "image.jpg\"", requestBody);
        //接口请求
        APIManager.getInstance().getUploadAPI().uploadFile(photos).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                if (data == null || data.getData() == null) {
                    MToast.showToast(mContext, "上传失败");
                    return;
                }
                urlString.postValue(data.getData().toString());
            }
        });
    }
}
